﻿using AuthTemplate.DTO.Module;
using AuthTemplate.Entity;
using AuthTemplate.Infrastructure;
using AuthTemplate.Infrastructure.Token;
using AuthTemplate.MVC.Filter;
using AuthTemplate.Service;
using AuthTemplate.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AuthTemplate.MVC.Controllers
{
    [LoginFilter]
    [RoutePrefix("module")]
    public class ModuleManagerController : BaseController
    {
        private readonly IModuleService moduleService;
        private readonly IModuleElementService moduleElementService;

        public ModuleManagerController(IModuleService moduleService,
            IModuleElementService moduleElementService)
        {
            this.moduleService = moduleService;
            this.moduleElementService = moduleElementService;
        }

        [HttpPost]
        [Route("loadMenu")]
        public string LoadMenu()
        {
            MyResponse<List<ModuleDto>> resp = new MyResponse<List<ModuleDto>>();
            try
            {
                string token = HttpContext.Request.Headers["Authorization"].Substring(7).Trim();
                string userId = CommonToken.DecodeToken(token, false).UserID;

                List<ModuleDto> moduleDtos = moduleService.LoadForUser(userId);
                resp.data = moduleDtos;
            }
            catch(Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }

            return JsonHelper.Serialize(resp);
        }

        [HttpGet]
        [Route("getAllModule")]
        public string GetAllModule(int page ,int limit ,string key)
        {
            TableData<List<Module>> resp = new TableData<List<Module>>();
            try
            {
                int count = 0;
                var modules = moduleService.GetModulePage( page,  limit ,out count);
                resp.data = modules;
                resp.code = 0;
                resp.msg = "操作成功";
                resp.count = count;
            }
            catch(Exception ex)
            {
                resp.code = 500;
                resp.msg = "操作失败" + ex.Message;
                resp.count = 0;
            }

            return JsonHelper.Serialize(resp);
        }

        [HttpPost]
        [Route("editModule")]
        public string EditModule(EditModuleInput input)
        {
            MyResponse resp = new MyResponse();
            try
            {
                input.Url = Tools.AnyToString(input.Url);
                input.ParentId = Tools.AnyToString(input.ParentId);
                input.ParentName = Tools.AnyToString(input.ParentName);
                moduleService.EditModule(input);
            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }

        [HttpPost]
        [Route("addModule")]
        public string AddModule(AddModuleInput input)
        {
            MyResponse resp = new MyResponse();
            try
            {
                input.Url = Tools.AnyToString(input.Url);
                input.ParentId = Tools.AnyToString(input.ParentId);
                input.ParentName = Tools.AnyToString(input.ParentName);
                moduleService.AddModule(input);
            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }

        [HttpPost]
        [Route("deleteModule")]
        public string DeleteModule(string  id)
        {
            MyResponse resp = new MyResponse();
            try
            {
                moduleService.DeleteModule(id);
            }
            catch (Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }
    }
}